
package ch.hearc.meteo.imp.afficheur.real;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Properties;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.InputVerifier;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class JDialogPreferencesRemote extends JDialog
	{

	/*------------------------------------------------------------------*\
	|*							Constructeurs							*|
	\*------------------------------------------------------------------*/
	public JDialogPreferencesRemote()
		{
		geometry();
		control();
		apparence();
		}

	/*------------------------------------------------------------------*\
	|*							Methodes Public							*|
	\*------------------------------------------------------------------*/

	/*------------------------------*\
	|*				Set				*|
	\*------------------------------*/

	/*------------------------------*\
	|*				Get				*|
	\*------------------------------*/

	public boolean isOk()
		{
		return this.isOk;
		}

	/*------------------------------------------------------------------*\
	|*							Methodes Private						*|
	\*------------------------------------------------------------------*/

	private void apparence()
		{
		this.setDefaultCloseOperation(HIDE_ON_CLOSE);
		this.setTitle("Propriétés");
		this.setModal(true);
		this.setSize(new Dimension(400, 200));
		this.setResizable(false);
		this.setVisible(true);

		}

	private void control()
		{
		ActionListener validateActionListener = new ActionListener()
			{

				@Override
				public void actionPerformed(ActionEvent event)
					{
					boolean test = true;

					test = test && txtProxyAddress.getInputVerifier().verify(txtProxyAddress);
					test = test && txtProxyPort.getInputVerifier().verify(txtProxyPort);

					if (test)
						{
						savePreferences();
						JDialogPreferencesRemote.this.dispose();

						}
					}
			};

		txtProxyAddress.addActionListener(validateActionListener);
		txtProxyPort.addActionListener(validateActionListener);
		txtServerPort.addActionListener(validateActionListener);
		btnOk.addActionListener(validateActionListener);

		InputVerifier inputVerifier = new InputVerifier()
			{

				@Override
				public boolean verify(JComponent component)
					{
					JTextField txtField = (JTextField)component;

					return !txtField.getText().equals("");
					}
			};

		txtProxyAddress.setInputVerifier(inputVerifier);
		txtProxyPort.setInputVerifier(new InputVerifier()
			{

				@Override
				public boolean verify(JComponent component)
					{
					JTextField txtField = (JTextField)component;
					try
						{
						int value = Integer.valueOf(txtField.getText());
						if (value >= -0 && value <= 65535)
							{
							return true;
							}
						else
							{
							return false;
							}
						}
					catch (Exception e)
						{
						return false;
						}
					}
			});
		txtServerPort.setInputVerifier(new InputVerifier()
			{

				@Override
				public boolean verify(JComponent component)
					{
					JTextField txtField = (JTextField)component;
					try
						{
						int value = Integer.valueOf(txtField.getText());
						if (value >= -0 && value <= 65535)
							{
							return true;
							}
						else
							{
							return false;
							}
						}
					catch (Exception e)
						{
						return false;
						}
					}
			});
		}

	private void geometry()
		{
		this.setLayout(new BoxLayout(this.getContentPane(), BoxLayout.Y_AXIS));

		JPanel panel1 = new JPanel();
		panel1.setLayout(new FlowLayout(FlowLayout.LEFT));
		JLabel lblServerPort = new JLabel("Port du serveur : ");
		txtServerPort = new JTextField();
		txtServerPort.setPreferredSize(new Dimension(50, 25));
		panel1.add(lblServerPort);
		panel1.add(txtServerPort);

		JPanel panel2 = new JPanel();
		panel2.setLayout(new FlowLayout(FlowLayout.LEFT));
		JLabel lblProxyAddress = new JLabel("Proxy : ");
		txtProxyAddress = new JTextField();
		txtProxyAddress.setPreferredSize(new Dimension(200, 25));
		JLabel lblProxyPort = new JLabel("Port : ");
		txtProxyPort = new JTextField();
		txtProxyPort.setPreferredSize(new Dimension(50, 25));
		checkBoxActivateProxy = new JCheckBox("Activer Proxy");
		panel2.add(lblProxyAddress);
		panel2.add(txtProxyAddress);
		panel2.add(lblProxyPort);
		panel2.add(txtProxyPort);

		btnOk = new JButton("Ok");

		this.add(Box.createVerticalStrut(10));
		this.add(panel1);
		this.add(Box.createVerticalStrut(10));
		this.add(panel2);
		this.add(Box.createVerticalStrut(10));
		this.add(checkBoxActivateProxy);
		this.add(Box.createVerticalStrut(10));
		this.add(btnOk);
		this.add(Box.createVerticalStrut(10));

		loadPreferences();
		}

	private void loadPreferences()
		{

		try
			{
			Properties p = new Properties();
			p.load(new FileInputStream(PROPERTY_FILENAME));
			this.txtServerPort.setText(p.getProperty(SERVER_PORT_PROPERTY_NAME));
			this.txtProxyAddress.setText(p.getProperty(PROXY_ADDRESS_PROPERTY_NAME));
			this.txtProxyPort.setText(p.getProperty(PROXY_PORT_PROPERTY_NAME));
			this.checkBoxActivateProxy.setSelected(Boolean.valueOf(p.getProperty(PROXY_ACTIVATED_PROPERTY_NAME)));
			this.setLocation(new Point(Integer.valueOf(p.getProperty(POS_X_PROPERTY_NAME)), Integer.valueOf(p.getProperty(POS_Y_PROPERTY_NAME))));

			}
		catch (Exception e)
			{

			}
		}

	private void savePreferences()
		{
		Properties p = new Properties();
		p.setProperty(SERVER_PORT_PROPERTY_NAME, txtServerPort.getText());
		p.setProperty(PROXY_ADDRESS_PROPERTY_NAME, txtProxyAddress.getText());
		p.setProperty(PROXY_PORT_PROPERTY_NAME, txtProxyPort.getText());
		p.setProperty(POS_X_PROPERTY_NAME, Integer.toString((int)this.getLocation().getX()));
		p.setProperty(POS_Y_PROPERTY_NAME, Integer.toString((int)this.getLocation().getY()));
		p.setProperty(PROXY_ACTIVATED_PROPERTY_NAME, Boolean.toString(checkBoxActivateProxy.isSelected()));
		File f = new File("remoteProperties.ini");
		OutputStream out;
		try
			{
			out = new FileOutputStream(f);
			p.store(out, "Meteo station properties");
			}
		catch (Exception e)
			{

			}

		isOk = true;

		}

	/*------------------------------------------------------------------*\
	|*							Attributs Private						*|
	\*------------------------------------------------------------------*/

	//Output
	private boolean isOk = false;

	// Tools
	private JCheckBox checkBoxActivateProxy;
	private JTextField txtProxyAddress;
	private JTextField txtProxyPort;
	private JTextField txtServerPort;
	private JButton btnOk;

	public static final String PROPERTY_FILENAME = "remoteProperties.ini";
	public static final String SERVER_PORT_PROPERTY_NAME = "serverPort";
	public static final String PROXY_ADDRESS_PROPERTY_NAME = "proxyAddress";
	public static final String PROXY_PORT_PROPERTY_NAME = "proxyPort";
	public static final String PROXY_ACTIVATED_PROPERTY_NAME = "activateProxy";
	public static final String POS_X_PROPERTY_NAME = "posX";
	public static final String POS_Y_PROPERTY_NAME = "posY";
	}
